package com.uuuo.ugamepluginsdk;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.view.View;

import com.uuuo.ugamepluginsdk.droidfix.DroidFix;
import com.uuuo.ugamepluginsdk.utils.FileUtil;

import java.io.File;
import java.io.IOException;

public class MainActivity extends BaseActivity {
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;


        new Thread(){
            @Override
            public void run() {
                super.run();
                Looper.prepare();
                Handler handler = new Handler();
            }
        }.start();

    }

    public void fixBug(View view) {
        performCodeWithPermission("请求权限",
                new PermissionCallback() {
                    @Override
                    public void hasPermission() {
//                        File fixFile = new File(Environment.getExternalStorageDirectory(), "fix.apk");
//                        if (fixFile.exists()) {
//                            FixBugManager manager = new FixBugManager(context);
//                            try {
//                                manager.fixDex(fixFile.getAbsolutePath());
//                                Toast.makeText(context, "修复成功!", Toast.LENGTH_SHORT).show();
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                                Toast.makeText(context, "修复失败!", Toast.LENGTH_SHORT).show();
//                            }
//                        }


                        File fixFile = new File(Environment.getExternalStorageDirectory(), "fix.apk");

                        File destDir = new File(getFilesDir(), DroidFix.DROID_CODE_CACHE);
                        if (!destDir.exists()) {
                            destDir.mkdirs();
                        }


                        File dest = new File(getFilesDir(), DroidFix.DROID_CODE_CACHE + File.separator + "fix.apk");
                        try {
                            if (!dest.exists()) {
                                FileUtil.copyFile(fixFile, dest);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if (dest.isFile() && dest.exists()) {
                            new DroidFix().installPatch(context, dest);
                        }

                    }

                    @Override
                    public void noPermission() {

                    }
                },
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);


    }

    public void initSDK(View view) {
        com.uuuo.gamesdk.UGameSDK.getInstance().init(context);
    }

    public void loginSDK(View view) {
        com.uuuo.gamesdk.UGameSDK.getInstance().login();
    }
}
